package com.atguigu.gmall.task.job;

import com.atguigu.gmall.rabbit.config.MqConst;
import com.atguigu.gmall.rabbit.service.RabbitService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author: atguigu
 * @create: 2023-08-15 11:32
 */
@Slf4j
@Component
public class TaskJob {

    @Autowired
    private RabbitService rabbitService;

    /**
     * spring定时任务-适用于单体项目中简单定时任务；任务无法停止，任务执行周期
     * cron表达式：秒 分 时 日 月 周 [年]
     */
    //@Scheduled(cron = "0/5 * * * * ?")
    public void stringTask() {
        System.out.println("任务执行了");
    }


    /**
     * 每日凌晨1点执行任务，发送消息通知秒杀服务缓存预热
     */
    @XxlJob("seckillImport")
    public void seckillImport() {
        log.info("[定时任务]发送秒杀商品入库消息");
        rabbitService.sendMsg(MqConst.EXCHANGE_DIRECT_TASK, MqConst.ROUTING_TASK_1, "干活了");
    }


    /**
     * 每日晚上18点执行任务，发送消息通知秒杀服务清空缓存
     */
    @XxlJob("seckillClean")
    public void seckillClean() {
        log.info("[定时任务]发送秒杀秒杀情况消息");
        rabbitService.sendMsg(MqConst.EXCHANGE_DIRECT_TASK, MqConst.ROUTING_TASK_18, "下班吧");
    }
}
